<?php

class DynamicController extends Controller {
	public function getCategoryListAction() {
		Response::ajax(200, 'success', self::model('dynamic_category')->getAllCategory());
	}

	public function listAction() {
		$requestData = [];
		$requestData['categoryId'] = intval(Request::getParams('categoryId'));
		$requestData['keywords'] = Security::removeXss(Request::getParams('keywords'));
		$requestData['page'] = intval(Request::getParams('page', 1));
		$requestData['pageSize'] = intval(Request::getParams('pageSize', 20));
		$requestData['uid'] = intval(Request::getParams('puid'));
		$requestData['tag'] = Request::getParams('tag');
		$requestData['tag_id'] = Request::getParams('tagId');
		$user_type = intval(Request::getParams('user_type', 1));
		$requestData['user_type'] = $user_type == 3 ? 2 : $user_type;
		$where = $value = [];
		if ($requestData['user_type'] == 2) {
			$where[] = 'status=?';
			$value[] = 1;
		}
		if ($requestData['categoryId']) {
			$where[] = 'category_id=?';
			$value[] = $requestData['categoryId'];
		}
		if ($requestData['keywords']) {
			$where[] = 'title like ?';
			$value[] = '%' . $requestData['keywords'] . '%';
		}
		// if ($requestData['tag']) {
		// 	$where[] = 'tag like ?';
		// 	$value[] = '%' . $requestData['tag'] . '%';
		// }
		if ($requestData['uid']) {
			$where[] = 'uid=?';
			$value[] = $requestData['uid'];
		}
		if ($requestData['tag_id']) {
			$where[] = 'tag_id=?';
			$value[] = $requestData['tag_id'];
		}
		if ($requestData['user_type']) {
			$where[] = 'user_type=?';
			$value[] = $requestData['user_type'];
		}
		if ($requestData['user_type'] == 1) {
			$where[] = 'showtime<?';
			$value[] = time();
		}

		$order = 'is_recommend desc, dynamic_id desc';
		Response::ajax(200, 'success', self::model('dynamic')->getList($where, $value, $order, $requestData['page'], $requestData['pageSize']));
	}

	public function infoAction() {
		$dynamicId = intval(Request::getParams('dynamicId'));
		if (!$dynamicId || !$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $dynamicId)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		Response::ajax(200, 'success', self::model('dynamic')->buildInfo($dynamicInfo));
	}

	public function addAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		Log::write('info' . $_POST, 'INFO');
		$dynamicId = intval(Request::getParams('dynamicId'));
		$data = [];
		$data['title'] = strval(Security::removeXss(Request::getParams('title')));
		$data['content'] = strval(Security::removeXss(Request::getParams('content')));
		$data['image'] = strval(Request::getParams('image'));
		$data['image_list'] = strval(Request::getParams('imageList'));
		$data['video_url'] = strval(Request::getParams('videoUrl'));
		$data['gif_image'] = strval(Request::getParams('gifImage'));
		$data['tag'] = strval(Request::getParams('tag'));
		$data['tag_id'] = intval(Request::getParams('tagId'));
		$data['uid'] = $userInfo['uid'];
		$data['bind_id'] = Request::getParams('bindId');
		//$data['category_id'] = Request::getParams('categoryId');
		$data['bind_type'] = intval(Request::getParams('bindType'));
		$data['user_type'] = intval(Request::getParams('userType', 1));
		$data['add_time'] = $data['update_time'] = date('Y-m-d H:i:s');
		if ($data['user_type'] == 2) {
			if ($userInfo['type'] == 1) {
				Response::ajax(NORMAL_ERROR, '成为店主或代理商才能发布大咖说');
			}
			$data['status'] = 0;
		} else {
			$data['status'] = 1;
		}
		try {
			if ($data['video_url']) {
				$data['type'] = dynamicModel::TYPE_VIDEO;
				// if (!$data['image']) {
				// 	$data['image'] = $data['video_url'] . Configure::get('oss.video.shot');
				// }
			} else {
				$data['type'] = dynamicModel::TYPE_ARTICLE;
			}
			if ($dynamicId) {
				if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $dynamicId)) {
					Response::ajax(NORMAL_ERROR, '动态文章信息获取失败,无法编辑');
				}
				if ($dynamicInfo['uid'] != $userInfo['uid']) {
					Response::ajax(NORMAL_ERROR, '动态文章信息获取失败,无法完成编辑');
				}
				if (self::model('dynamic')->update($data, 'dynamic_id=?', $dynamicId)) {
					Response::ajax(200, '更新成功');
				}
			} else {
				if ($dynamicId = self::model('dynamic')->insert($data, true)) {
					self::model('user')->execute("update wf_user set dynamic_total=dynamic_total+1 where uid={$userInfo['uid']}");
					Response::ajax(200, '添加成功');
				}
			}
		} catch (Exception $exc) {
			Log::write('添加文章失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(SYSTEM_ERROR, '添加失败');
	}

	public function shareSuccessAction() {
		$dynamicId = intval(Request::getParams('dynamicId'));
		if (self::model('dynamic')->execute("update wf_dynamic set share_total=share_total+1 where dynamic_id={$dynamicId}")) {
			Response::ajax(200, 'success');
		}
		Response::ajax(NORMAL_ERROR, 'fail');
	}

	public function deleteAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$dynamicId = intval(Request::getParams('dynamicId'));
		if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $dynamicId)) {
			Response::ajax(NORMAL_ERROR, '动态文章信息获取失败,无法删除');
		}
		if ($dynamicInfo['uid'] != $userInfo['uid']) {
			Response::ajax(NORMAL_ERROR, '动态文章信息获取失败,无法完成删除');
		}
		try {
			if (self::model('dynamic')->delete('dynamic_id=?', $dynamicId)) {
				self::model('user')->execute("update wf_user set dynamic_total=dynamic_total-1 where uid={$userInfo['uid']}");
				Response::ajax(200, '删除成功');
			}
		} catch (Exception $exc) {
			Log::write('删除动态失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(SYSTEM_ERROR, '删除失败,请稍后');
	}

	public function addViewAction() {
		$dynamicId = intval(Request::getParams('dynamicId'));
		if (self::model('dynamic')->execute("update wf_dynamic set view_total=view_total+1 where dynamic_id={$dynamicId}")) {
			Response::ajax(200, 'success');
		}
		Response::ajax(NORMAL_ERROR, 'fail');
	}

	public function addUpAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$userInfo = self::model('user')->findById($userInfo['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$dynamicId = intval(Request::getParams('dynamicId'));
		if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $dynamicId)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$type = intval(Request::getParams('type', 1));
		try {
			if (self::model('dynamic_up')->addUp($userInfo['uid'], $dynamicId)) {
				user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_UP, $dynamicId, $userInfo);
				self::model('dynamic')->execute("update wf_dynamic set up_total=up_total+1 where dynamic_id={$dynamicId}");
				Response::ajax(200, 'success');
			}
		} catch (Exception $exc) {
			Log::write('点赞失败:' . $exc->getTraceAsString(), 'WARN');
			Response::ajax(200, 'success');
		}

		Response::ajax(NORMAL_ERROR, 'fail');
	}

	public function isUpAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$dynamicId = trim(Request::getParams('dynamicId'));
		Response::ajax(200, 'success',
			self::model('dynamic_up')->isUp($userInfo['uid'], $dynamicId));
	}

	public function removeUpAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$dynamicId = intval(Request::getParams('dynamicId'));
		try {
			if (self::model('dynamic_up')->removeUp($userInfo['uid'], $dynamicId)) {
				self::model('dynamic')->execute("update wf_dynamic set up_total=up_total-1 where dynamic_id={$dynamicId}");
				Response::ajax(200, 'success');
			}
		} catch (Exception $exc) {
			Log::write('取消点赞失败:' . $exc->getTraceAsString(), 'WARN');
			Response::ajax(200, 'success');
		}
		Response::ajax(NORMAL_ERROR, 'fail');
	}

	public function relateAction() {
		$dynamicId = intval(Request::getParams('dynamicId'));
		$categoryId = intval(Request::getParams('categoryId'));
		Response::ajax(200, 'success', self::model('dynamic')->getRelateDynamicList($dynamicId, $categoryId, 4));
	}

	public function taglistAction() {
		$tag = self::model('tag')->getList('id desc');
		$lists = [];
		foreach ($tag['list'] as $key => $value) {
			$lists[$key]['id'] = intval($value['id']);
			$lists[$key]['title'] = Html::decode($value['title']);
			$lists[$key]['image'] = strval($value['image']);
			$lists[$key]['content'] = Html::decode($value['content']);
			$lists[$key]['abstract'] = strval($value['abstract']);
			$lists[$key]['viewtTotal'] = intval($value['view_total']);
			$lists[$key]['addtime'] = date('Y-m-d', $value['addtime']);
		}
		Response::ajax(200, 'success', $lists);
	}
	public function tagviewAction() {

		$id = intval(Request::getParams('id', 1));
		if (!$info = self::model('tag')->getOne('id=?', $id)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$data1['view_total'] = $info['view_total'] + 1;
		self::model('tag')->update($data1, 'id=?', $id);

		Response::ajax(200, 'success', '浏览成功');
	}

	public function taginfoAction() {

		$id = intval(Request::getParams('id', 1));
		if (!$info = self::model('tag')->getOne('id=?', $id)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$data['id'] = intval($info['id']);
		$data['title'] = Html::decode($info['title']);
		$data['image'] = strval($info['image']);
		$data['content'] = Html::decode($info['content']);
		$data['abstract'] = strval($info['abstract']);
		$data['viewtTotal'] = intval($info['view_total']);
		$data['addtime'] = date('Y-m-d', $info['addtime']);
		Response::ajax(200, 'success', $data);
	}
	public function activityAction() {
		$user_info = self::model('user')->checkLogin();
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		Response::ajax(200, 'success', self::model('activity')->getList('id desc', $user_info['uid'], $page, $pageSize));
	}

	public function activityinfoAction() {
		$user_info = self::model('user')->checkLogin();
		$id = intval(Request::getParams('id', 1));
		if (!$info = self::model('activity')->getOne('id=?', $id)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$data['is_fans'] = 0;
		if ($user_info) {
			$relate = self::model('activity_up')->getAll('uid=?', $user_info['uid']);
			$temp = [];
			if ($relate) {
				foreach ($relate as $key => $value) {
					$temp[] = $value['activity_id'];
				}
				if (in_array($id, $temp)) {
					$data['is_fans'] = 1;
				}
			}
		}
		$data['id'] = intval($info['id']);
		$data['title'] = strval($info['title']);
		$data['image'] = strval($info['image']);
		$data['content'] = Html::decode($info['content']);
		$data['abstract'] = $info['abstract'];
		$data['commentTotal'] = intval($info['comment_total']);
		$data['upTotal'] = intval($info['up_total']);
		$data['shareTotal'] = intval($info['share_total']);
		$data['status'] = intval($info['status']);
		$data['addtime'] = date('Y-m-d', $info['addtime']);
		Response::ajax(200, 'success', $data);
	}

	public function activityUpAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$userInfo = self::model('user')->findById($userInfo['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$id = intval(Request::getParams('id'));
		if (!$dynamicInfo = self::model('activity')->getOne('id=?', $id)) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$type = Request::getParams('type', 1);
		try {
			if (self::model('activity_up')->addUp($userInfo['uid'], $id, $type)) {
				//user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_UP, $dynamicId, $userInfo);
				if ($type == 1) {
					self::model('activity')->execute("update wf_activity set up_total=up_total+1 where id={$id}");
				} else {
					self::model('activity')->execute("update wf_activity set up_total=up_total-1 where id={$id}");
				}

				Response::ajax(200, 'success');
			}
		} catch (Exception $exc) {
			Log::write('点赞失败:' . $exc->getTraceAsString(), 'WARN');
			Response::ajax(200, 'success');
		}

		Response::ajax(NORMAL_ERROR, 'fail');
	}
}